package Judge.Login.Model;

import Core.Model.ClientType;
import Exception.LoginFailureException;
import Exception.SocketConnectionFailureException;
import Judge.Login.View.*;
import Judge.Login.Model.*;
import Judge.Login.Controller.*;
import Judge.Login.Model.InitializeSocket;
import Judge.Login.Model.ServerConnection;

import java.net.*;

public class JudgeLoginModel 
{
    public void initialize()
    {
    	
    }
    public void setViewAndController(JudgeLoginView view, JudgeLoginController controller)
    {
    	this.view = view;
    	this.controller = controller;
    }
    
    public boolean login(String userName, char[] password) 
                           throws SocketConnectionFailureException, LoginFailureException 
    {
		try 
		{
			InitializeSocket setSocket = new InitializeSocket(this);
			if (setSocket.initializeSocket() == true) 
			{
				ServerConnection serverConnection = new ServerConnection(this.socket, ClientType.JUDGE);
				try 
				{
					if (serverConnection.login(userName, new String(password))) 
					{
						return true;
					}
				} 
				catch (LoginFailureException exception) 
				{
					throw exception;
				}
			}
		}
		catch (SocketConnectionFailureException exception) 
		{
			throw exception;
		}
		return true;
	}
    
    
    public Socket socket;
    private JudgeLoginView view;
    private JudgeLoginController controller;
}
